/*
 * 
 * Created on Jul 30, 2005
 */
package remato.client.view.requirementstable;

import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;

import remato.client.model.AbstractPropertySpesification;
import remato.client.model.ClientModelItem;
import remato.client.model.facade.IPropertiesContainer;

/**
 * 
 * @author Petter L. H. Eide
 * @version $Id: TableColumnSorter.java,v 1.1 2006-01-23 19:58:37 petterei Exp $
 * 
 * Changelog:
 * 
 * $Log: TableColumnSorter.java,v $
 * Revision 1.1  2006-01-23 19:58:37  petterei
 * initial commit for client, fitting the alternative session implementation
 *
 * Revision 1.4  2006-01-03 13:33:02  petterei
 * adjustments and finery
 *
 * Revision 1.3  2005-12-05 04:16:41  petterei
 * *** empty log message ***
 *
 * Revision 1.2  2005/10/24 16:25:36  petterei
 * *** empty log message ***
 *
 * Revision 1.1  2005/10/11 15:06:59  petterei
 * Seccond iteration:
 * 1. Eclipse based client with new domain model
 * 2. Flexible Project and Category hierarchy with Drag-n-Drop functionality
 * New functionality:
 * 3. Eclipse plugin update-site
 *
 * Revision 1.2  2005/08/26 15:11:45  peide3
 * fixed bug in properties view (using propertyspecification as id, instead of object)
 *
 * Revision 1.1  2005/08/23 06:38:13  peide3
 * *** empty log message ***
 *
 * Revision 1.1  2005/07/31 22:55:10  petterei
 * initial commit
 *
 *
 */
public class TableColumnSorter extends ViewerSorter {

    private int index = 0;
    private List<AbstractPropertySpesification> spesifications;
    private String[] columnNames;
    

    public TableColumnSorter(List<AbstractPropertySpesification> spesifications, String[] columnNames) {
        super();
        this.spesifications = spesifications;
        this.columnNames = columnNames;
    }

    /**
     * @see org.eclipse.jface.viewers.ViewerSorter#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
     */
    @Override
    public int compare(Viewer viewer, Object e1, Object e2) {
        Object ee1;
        Object ee2;
        if (e1 instanceof ClientModelItem && e2 instanceof ClientModelItem) {
            AbstractPropertySpesification id = getPropertySpecification();
            ee1 = ((IPropertiesContainer) e1).getCurrentPropertyValue(id).getCellEditorValue();
            ee2 = ((IPropertiesContainer) e2).getCurrentPropertyValue(id).getCellEditorValue();
        } else {
            ee1 = e1;
            ee2 = e2;
        }
        return super.compare(viewer, ee1, ee2);
    }

    /**
     * @see org.eclipse.jface.viewers.ViewerSorter#isSorterProperty(java.lang.Object, java.lang.String)
     */
    @Override
    public boolean isSorterProperty(@SuppressWarnings("unused") Object element, String property) {
        boolean affected = false;
        int currentIndex = Arrays.asList(columnNames).indexOf(property);
        if (this.index == currentIndex) {
            affected = true;
        }
        return affected;
    }
    
    /**
     * @param index The index to set.
     */
    public void setIndex(int index) {
        this.index = index;
    }

    private AbstractPropertySpesification getPropertySpecification() {
        return spesifications.get(index);
    }
}
